<template>
    <div class="app-container">
        <div class="head">
            <img src="@/assets/img/Layout/position.png" />
            <p class="see-block">退料单详情</p>
        </div>
        <div class="view-container">
            <el-row class="status-title">
                <el-col class="mr20 width100" v-if="detailData.approveStatus == '0'">
                    <el-button
                        class="btn-default width100"
                        type="primary"
                        @click="audit()"
                        v-hasPermi="['system:materialReturn:checkReturn']"
                    >审核</el-button>
                </el-col>
                <el-col class="mr20 width100" v-if="(detailData.approveStatus == '0' || detailData.approveStatus == '2' || detailData.approveStatus == null) && detailData.status == '0' || detailData.status == '1'">
                    <el-button
                        class="btn-default width100"
                        type="primary"
                        @click="edit()"
                        v-hasPermi="['system:materialReturn:edit']"
                    >编辑</el-button>
                </el-col>
                <el-col class="mr20 width100" v-if="detailData.approveStatus == '1' && detailData.status == '2'">
                    <el-button
                        class="btn-default width100"
                        type="primary"
                        @click="confirm()"
                    >确定</el-button>
                </el-col>
                <el-col :span="10" class="status-container">
                    <div class="status-item">
                        <span :class="{ 'active' : detailData.status == '0'}">草稿</span>
                        <span :class="{ 'active' : detailData.status == '1'}">待审核</span>
                        <span :class="{ 'active' : detailData.status == '2'}">待退料</span>
                        <span :class="{ 'active' : detailData.status == '3'}">已退料</span>
                    </div>
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="9">
                    <span>单据编码：</span>{{ detailData.code ?  detailData.code : '暂无'}}
                </el-col>
                <el-col :span="9">
                    <span>业务日期：</span>{{ detailData.businessDate ?  detailData.businessDate : '暂无'}}
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="9">
                    <span>业务部门：</span>{{ detailData.businessUnit ?  detailData.businessUnit : '暂无'}}
                </el-col>
                <el-col :span="9">
                    <span>业务类型：</span>{{ detailData.businessType == '0' ? '生产退料' : '暂无'}}
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="9">
                    <span>原领料单：</span>{{ detailData.materialRequisitionCode ?  detailData.materialRequisitionCode : '暂无'}}
                </el-col>
                <el-col :span="9">
                    <span>是否整单退：</span>{{ detailData.ifWholeOrder == '0' ? '是' : '否'}}
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="9">
                    <span>接收仓库：</span>{{ detailData.receivingWarehouse ?  detailData.receivingWarehouse : '暂无'}}
                </el-col>
                <el-col :span="9">
                    <span>退货原因：</span>{{ detailData.reasonsForReturn ? detailData.reasonsForReturn : '暂无'}}
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>创建人：</span>{{ detailData.createBy ?  detailData.createBy : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>创建时间：</span>{{ detailData.createTime ?  detailData.createTime : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>退料人：</span>{{ detailData.returnerName ?  detailData.returnerName : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>审核人：</span>{{ detailData.approveName ?  detailData.approveName : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>审核时间：</span>{{ detailData.approveTime ?  detailData.approveTime : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>状态：</span>
                            <font v-if="detailData.status == 1" style="color:#13C2C2">待审核</font>
                            <font v-if="detailData.status == 2" style="color:#13C2C2">待退料</font>
                            <font v-if="detailData.status == 3" style="color:#52C41A">已退料</font>
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>备注：</span>{{ detailData.remark ?  detailData.remark : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="24">
                    <el-table :data="detailData.detailList">
                        <el-table-column label="序号" type="index" align="center" width="50" />
                        <el-table-column :label="item.label" :prop="item.prop" :width="item.width" v-for="(item, index) in columns" :key="index" align="center">
                            <template slot-scope="scope">
                                <template v-if="detailData.approveStatus == '1' && detailData.status == '2'">
                                    <template v-if="item.type == 'realNum'">
                                        <template v-if="scope.row.scanCode == '1'">
                                            <el-input placeholder="请输入数量" v-model="scope.row[item.prop]" @input="(val) => inputNum(val, scope.row)" @blur="saveData(scope.row)" :disabled="scope.row.positionList ? true : false" />
                                        </template>
                                        <template v-else>
                                            {{ scope.row[item.prop] }}
                                        </template>
                                    </template>
                                    <template v-if="item.type == 'more'">
                                        <el-tooltip v-if="scope.row.positionList !== null" effect="dark" content="多仓位" placement="bottom" >
                                            <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.row)"></span>
                                        </el-tooltip>
                                    </template>
                                    <span v-if="item.type == '1'">{{ scope.row[item.prop] }}</span>
                                </template>
                                <template v-else>
                                    <span>{{ scope.row[item.prop] }}</span>
                                    <template v-if="scope.row.positionList !== null">
                                        <el-tooltip v-if="item.type == 'more'" effect="dark" content="多仓位" placement="bottom" >
                                            <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.row)"></span>
                                        </el-tooltip>
                                    </template>
                                </template>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-col>
            </el-row>
        </div>
        <!-- 审核 -->
        <el-dialog :visible.sync="open" width="500" top="100px" append-to-body title="审核意见" center>
            <el-row>
                <el-col :span="24">
                    <textarea
                        class="approve-textarea"
                        v-model="approveRemark"
                        type="textarea"
                        placeholder="请输入意见和看法"
                    />
                </el-col>
            </el-row>
            <div slot="footer" class="dialog-footer">
                <el-button
                    type="primary"
                    class="btn-success mr20 width100"
                    @click="agreeApprove()"
                >同意</el-button>
                <el-button
                    type="primary"
                    class="btn-cancel width100"
                    @click="disagreeApprove()"
                >拒绝</el-button>
            </div>
        </el-dialog>
        <!-- 多仓位 -->
        <el-dialog :visible.sync="morePosition" width="800px" top="100px" append-to-body center :close-on-click-modal='false' :close-on-press-escape='false' :show-close="false">
            <el-table :data="moreData">
                <el-table-column label="名称" align="center" prop="productName" width="180" />
                <el-table-column label="可退数量" align="center" prop="purchaseNum" width="80" />
                <el-table-column label="申退数量" align="center" prop="planNum" width="80" />
                <el-table-column label="实退数量" align="center" prop="endNum" width="100">
                    <template slot-scope="scope">
                        <template v-if="detailData.approveStatus == '1' && detailData.status == '2'">
                            <template v-if="scope.row.scanCode == '0'">
                                <span>{{ scope.row.endNum }}</span>
                            </template>
                            <template v-else>
                                <el-input placeholder="请输入" v-model="scope.row.endNum" @input="(val) => inputEditNum(val, scope.row)" @blur="(val) => onBlurEditNum(val, scope.row)" />
                            </template>
                        </template>
                        <template v-else>
                            <span>{{ scope.row.endNum }}</span>
                        </template>
                    </template>
                </el-table-column>
                <el-table-column label="仓库" align="center" prop="warehouse">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.warehouse" disabled>
                            <el-option
                                v-for="item in warehouseList"
                                :key="item.warehouseId"
                                :label="item.name"
                                :value="item.warehouseId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="仓位" align="center" prop="positionName" />
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" class="btn-success width100" @click="onHideMorePosition">关闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { materialReturnEditSingle, materialReturnDetail, morePositionList, morePositionEdit, materialReturnCheckReturn, confirmReturn } from "@/api/system/warehouse/materialReturn"
import { queryWarehouse } from "@/api/system/warehouse/warehouse"
export default {
    name: "detail",
    data() {
        return {
            id: '',
            detailData: '',
            warehouseList: [], // 仓库列表
            // 表头
            columns: [{
                prop: "productCode",
                label: "物料编码",
                width: 150,
                type: '1'
            },{
                prop: "productName",
                label: "物料名称",
                type: '1',
                width: 150,
            },{
                prop: "productSpace",
                label: "物料规格",
                type: '1',
                width: 120,
            },
            // {
            //     prop: "batchNumber",
            //     label: "批次号",
            //     width: 150,
            //     type: '1'
            // },
            {
                prop: "picksNum",
                label: "可退数量",
                width: 100,
                type: '1'
            },{
                prop: "planNum",
                label: "申退数量",
                width: 120,
                type: '1'
            },{
                prop: "realNum",
                label: "退货数量",
                width: 120,
                type: 'realNum'
            },{
                prop: "unit",
                label: "单位",
                width: 100,
                type: '1'
            },{
                prop: "warehouse",
                label: "仓库",
                width: 150,
                type: '1'
            },{
                prop: "positionName",
                label: "仓位",
                width: 200,
                type: '1'
            },{
                prop: "remark",
                label: "备注",
                width: 200,
                type: '1'
            },{
                label: "",
                width: 60,
                type: 'more'
            }],
            open: false,
            approveRemark: '',
            morePosition: false,
            moreData: [],
        }
    },
    created(){
        this.id = this.$route.query.id
        this.getDetailData()
    },
    methods: {
        // 获取详情数据
        getDetailData(){
            materialReturnDetail(this.id).then(res => {
                this.detailData = res.data
                this.getWarehouseList()
            })
        },
        // 获取仓库列表
        getWarehouseList(){
            queryWarehouse().then(res => {
                if(res.code == 200){
                    this.warehouseList = res.data
                    // 接收仓库id转换
                    let arr = res.data.find(e => {
                        return e.warehouseId == this.detailData.receivingWarehouse
                    })
                    if(arr != undefined){
                        this.detailData.receivingWarehouse = arr.name
                    }
                    // 产品仓库id转换
                    if(this.detailData.detailList){
                        this.detailData.detailList.forEach(i => {
                            // console.log(i)
                            if(i.warehouse !== null || i.warehouse !== ''){
                                let dataList = res.data.find(item => {
                                    return item.warehouseId == i.warehouse
                                })
                                if(dataList != undefined){
                                    i.warehouse = dataList.name
                                }
                            }
                        })
                    }
                }
            })
        },
        // 审核
        audit(){
            this.open = true
        },
        // 通过
        agreeApprove(){
            this.getWarehouseConversion()
            let data = this.detailData
            data.approveStatus = 1
            data.status = 2
            materialReturnCheckReturn(this.detailData).then(res => {
                if(res.code == 200){
                    this.open = false
                    this.$modal.msgSuccess("操作成功")
                    this.$tab.closeOpenPage({ path: "/material/materialReturn" })
                }
            })
        },
        // 拒绝
        disagreeApprove(){
            this.getWarehouseConversion()
            let data = this.detailData
            data.approveStatus = 2
            data.status = 1
            materialReturnCheckReturn(data).then(res => {
                if(res.code == 200){
                    this.open = false
                    this.$modal.msgSuccess("操作成功")
                    this.$tab.closeOpenPage({ path: "/material/materialReturn" })
                }
            })
        },
        // 编辑
        edit(){
            this.$tab.closeOpenPage({ path: "/material/materialReturn/edit", query:{id: this.id} })
        },
        // 数量验证
        inputNum(val, row){
            if(val == '' || val == undefined){
                this.$modal.msgError('退货数量不能为空!')
            }else{
                if(/^[1-9]\d*$|^0$/.test(val) == false){
                    this.$modal.msgError('退货数量必须是数字！')
                    row.realNum = ''
                    return false
                }else{
                    if(JSON.parse(val) <= 0){
                        row.realNum = ''
                        this.$modal.msgError('退货数量必须大于0!')
                    }else{
                        if(JSON.parse(val) > row.planNum){
                            row.realNum = ''
                            this.$modal.msgError('退货数量不能大于申退数量，请重新输入!')
                        }
                    }
                }
            }
        },
        // 保存数量
        saveData(row){
            materialReturnEditSingle({id: row.id, realNum: row.realNum }).then(res => {
                if(res.code == 200){
                    this.getDetailData()
                }
            })
        },
        // 多仓位
        showMorePosition(row){
            morePositionList(row.id).then(res => {
                if(res.code == 200){
                    this.morePosition = true
                    this.moreData = res.rows
                }
            })
        },
        // 多仓位数量验证
        inputEditNum(val, row){
            if(row.areaId !== null ){
                if(val == '' || val == undefined){
                    this.$modal.msgError('实退数量不能为空!')
                }else{
                    if(/^[1-9]\d*$|^0$/.test(val) == false){
                        this.$modal.msgError('实退数量必须是数字！')
                        row.endNum = ''
                        return false
                    }else{
                        if(JSON.parse(val) <= 0){
                            row.endNum = ''
                            this.$modal.msgError('实退数量必须大于0!')
                        }else{
                            if(JSON.parse(val) > row.planNum){
                                row.endNum = ''
                                this.$modal.msgError('实退数量不能大于申退数量，请重新输入!')
                            }
                        }
                    }
                }
            }else{
                row.endNum = ''
                this.$modal.msgError('请选择仓位!')
            }
        },
        // 多仓位数据保存
        onBlurEditNum(val, row){
            if(val.target.value == null || val.target.value == ''){
                this.$modal.msgError('实退数量不能为空!')
            }else{
                let num = 0
                this.moreData.forEach(item => {
                    num += Number(item.endNum)
                    return num
                })
                if(num > row.planNum){
                    row.endNum = ''
                    this.$modal.msgError('实退数量不能大于申退数量!')
                }else{
                    morePositionEdit(row).then(res => {
                        if(res.code == 200){
                            this.getDetailData()
                        }
                    })
                }
            }
        },
        // 关闭多仓位
        onHideMorePosition(){
            this.morePosition = false
            if(this.detailData.approveStatus == '1' && this.detailData.status == '1'){
                this.getDetailData()
            }
        },
        // 产品仓库转换
        getWarehouseConversion(){
            let arr = this.warehouseList.find(e => {
                return e.name == this.detailData.receivingWarehouse
            })
            if(arr != undefined){
                this.detailData.receivingWarehouse = arr.warehouseId
            }
            this.detailData.detailList.forEach(i => {
                if(i.warehouse !== null || i.warehouse !== ''){
                    let dataList = this.warehouseList.find(item => {
                        return item.name == i.warehouse
                    })
                    if(dataList != undefined){
                        i.warehouse = dataList.warehouseId
                    }
                }
            })
        },
        // 确定
        confirm(){
            this.getWarehouseConversion()
            let data = this.detailData
            data.status = 3
            confirmReturn(this.detailData).then(res => {
                if(res.code == 200){
                    this.$modal.msgSuccess("退料成功")
                    this.$tab.closeOpenPage({ path: "/material/materialReturn" })
                }
            })
        }
    }
}
</script>

<style scoped>
::v-deep .el-input .el-input-group__append{
  padding: 0 10px;
}
.more{
  font-size: 30px;
  cursor: pointer;
}
::v-deep .el-dialog__header{
  display: none;
}
.icon-more-position{
  font-size: 22px;
  margin: 0 5px;
  cursor: pointer;
}
</style>